package TestDemo4;

import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;

public class Test {
    public static ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> ret=new ArrayList();
        PriorityQueue<Integer> queue=new PriorityQueue<>((o1, o2) -> o2 - o1);
        if(k == 0) return ret;
        for(int i=0;i<k;i++){
            queue.offer(input[i]);
        }
        for (int i = k; i < input.length; i++) {
            if(queue.peek() > input[i]){
                queue.poll();
                queue.add(input[i]);
            }
        }

        while(!queue.isEmpty()){
            ret.add(queue.poll());
        }

        return ret;

    }

    public static void main(String[] args) {
        int[] arr={4,5,1,6,2,7,3,8};
        ArrayList<Integer> ret=GetLeastNumbers_Solution(arr,4);
    }
}

